/**************************************************************************************
 *  Copyright (c)  2010  Pablo Echeverri, Changmin Bae.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package data.tileables;

/**************************************************************************************
 * Author(s): Pablo Echeverri, Changmin Bae
 * Resources
 **************************************************************************************/
public class Resources extends Doodad
{
	private final int FOOD = 0, METAL = 1, ENERGY = 2;
	
	private int food;
	private final int maxFood;
	private int ore;
	private int energy;
	private int typeHarvested; //0=food, 1=ore, 2=energy
	private Structure structure;
	private int workerCount;
	private boolean discovered;
	
		
	/**************************************************************************************
	 * Author(s): Pablo Echeverri
	 * Resources constructor
	 **************************************************************************************/
	public Resources(int aFood, int aOre, int aEnergy)
	{
		super();
		this.setType("Resources");
		this.food= this.maxFood = aFood;
		this.ore=aOre;
		this.energy=aEnergy;
		this.structure=null;
		this.workerCount=0;
		this.typeHarvested=-1;
	}
	/**************************************************************************************
	 * Author(s): Pablo Echeverri
	 * Accessor methods
	 **************************************************************************************/
	public int getFoodAmount()
	{
		return this.food;
	}
	public int getOreAmount()
	{
		return this.ore;
	}
	public int getEnergyAmount()
	{
		return this.energy;
	}
	public int getWorkerCount()
	{
		return this.workerCount;
	}
	public int getTypeHarvested()
	{
		return this.typeHarvested;
	}
	public Structure getStructure()
	{
		return this.structure;
	}
	
	/**************************************************************************************
	 * Author(s): Pablo Echeverri, Changmin Bae and Eric Martin-Taylor
	 * decrements the harvested value by amount in parameter
	 * Will not overharvest and will fill up the food by the max value
	 **************************************************************************************/
	public int harvest(int amount)
	{
		int temp = 0;
		if(this.typeHarvested==FOOD){ //gather food
			if(food - amount < 0){
				temp = food;
				food = 0;
				food = maxFood;
			}
			else{
				temp = food;
				food -= amount;
				food = maxFood;
			}
			

		}
		else if (this.typeHarvested==METAL){ //harvest ore
			if(ore - amount < 0){
				temp = ore;
				ore = 0;
			}
			else{
				temp = ore - amount;
				ore -= amount;
			}
		}
		else if(this.typeHarvested==ENERGY)//harvest energy
			if(food - amount < 0){
				temp = energy;
				energy = 0;
			}
			else{
				temp = energy - amount;
				energy -= amount;
			}
		
		return temp;
	}
	/**************************************************************************************
	 * Author(s): Pablo Echeverri, Changmin Bae and Eric Martin-Taylor
	 * Assign structure to harvest resource
	 **************************************************************************************/
	public boolean assignStructure(Structure aStructure)
	{
		if(this.structure==null){
			this.structure=aStructure;
			return true;
		}
		return false;
	}
	/**************************************************************************************
	 * Author(s): Pablo Echeverri, Changmin Bae
	 * set number of workers on that resource
	 **************************************************************************************/
	public void assignWorkers(int aWorkerCount)
	{
		this.workerCount+=aWorkerCount;
	}
	
	/**************************************************************************************
	 * Author(s): Pablo Echeverri, Changmin Bae
	 * decrement number of workers on resource and returns freed worker count
	 **************************************************************************************/
	public int unassignWorkers(int unWorker)
	{
		int freedWorkers = 0;
		if(unWorker>=this.workerCount)
		{
			freedWorkers += this.workerCount;
			this.workerCount=0;
			this.unassignStructure();
			return freedWorkers;
		}
		else
		{
			freedWorkers += unWorker;
			this.workerCount=this.workerCount-unWorker;
			return freedWorkers;
		}
	}
	/**************************************************************************************
	 * Author(s): Pablo Echeverri, Changmin Bae and Eric Martin-Taylor
	 * Unassign a structure and remove workers
	 **************************************************************************************/
	public void unassignStructure(){
		this.structure=null;
		this.workerCount = 0;
		this.setTypeHarvested(0);
	}
	
	/**************************************************************************************
	 * Author(s): Pablo Echeverri
	 * set type harvested to food, ore, or energy
	 **************************************************************************************/
	public void setTypeHarvested(int aTypeHarvested){
		this.typeHarvested=aTypeHarvested;
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * returns the array of image file names depending on what resources are present
	 **************************************************************************************/
	public String[] getImageData()
	{
		return new String[]{(this.food==0 ? "" : "res_food"),(this.ore==0 ? "" : "res_ore"),(this.energy==0 ? "" : "res_energy")};
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * returns the array of amounts of resources
	 **************************************************************************************/
	public String[] getResourceData()
	{
		return new String[]{(this.food==0 ? "" : ""+this.food),(this.ore==0 ? "" : ""+this.ore),(this.energy==0 ? "" : ""+this.energy)};
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * returns the array of number of workers assigned
	 **************************************************************************************/
	public String[] getWorkersData(int playerID)
	{
		if((this.structure!=null) && (this.structure.getPlayerID()==playerID))
		{
			return new String[]{(this.typeHarvested==0 ? ""+this.workerCount : ""),(this.typeHarvested==1 ? ""+this.workerCount : ""),(this.typeHarvested==2 ? ""+this.workerCount : "")};
		}
		else
		{
			return new String[]{"","",""};
		}
	}
	
	public String toString()
	{
		String toReturn = "";
		toReturn += "food = " + food + "\n";
		toReturn += "metal = " + ore + "\n";
		toReturn += "energy = " + energy + "\n";
		return toReturn;
	}
}
	
	
			
